/**    
 * @{#} SearchActivity.java Create on 2014-6-10 下午1:35:57    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class SearchPatientActivity extends BasicActivity {

	private EditText edit_keyword;

	boolean isDoctor;
	TextView text_tip;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.person_home);

		init();

		setNavTitle("用户主页");

	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.text_right) {
			Intent intent = new Intent(activity, ScanActivity.class);
			startActivityForResult(intent, 0);
		} else if (id == R.id.btn_search) {
			String keyword = edit_keyword.getText().toString();
			if (!CommonUtility.isNull(keyword)) {
				Intent intent = getIntent().setClass(activity,
						SearchResultActivity.class);
				intent.putExtra(Constant.IDENTITY_KEY, keyword);
				startActivity(intent);
			} else {
				CommonUtility.tip(activity, "请输入关键字进行搜索");
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		try {
			if (!CommonUtility.isNull(data)) {
				String info = data.getStringExtra(Constant.IDENTITY_KEY);
				JSONObject object = new JSONObject(info);
				final int userType = object.getInt("User_type");
				HashMap<String, Object> params = new HashMap<String, Object>();
				params.put(Constant.PARAM_API, "User.Get");
				params.put("uids", object.getInt("Uid"));
				RequestTaskIntercept.requestIntercept(activity,
						Constant.URL.REQUEST, new RequestHandler(activity,
								new HandlerCallback() {

									@Override
									public void callback(Object o) {
										if (!CommonUtility.isNull(o)) {
											try {
												JSONObject object = (JSONObject) o;
												JSONArray array = object
														.getJSONArray(Constant.JSON_PARAM_RES);
												if (array.length() > 0) {
													object = array
															.getJSONObject(0);
													if (userType == 2) {
														Intent intent = new Intent(
																activity,
																DoctorDetailActivity.class);
														intent.putExtra(
																Constant.IDENTITY_KEY,
																object.toString());
														startActivity(intent);
													} else if (userType == 1) {
														Intent intent = new Intent(
																activity,
																QuestionDetailActivity.class);
														intent.putExtra(
																Constant.IDENTITY_KEY,
																object.toString());
														intent.putExtra(
																Constant.IDENTITY_KEY_1,
																true);
														startActivity(intent);
													}
												}
											} catch (Exception e) {
												e.printStackTrace();
											}
										}
									}
								}), params);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
